package com.chijun.mform.field.impl;


import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chijun.mform.R;
import com.chijun.mform.create.mode.FormField;
import com.chijun.mform.field.BaseField;
import com.example.ccj.b.a.LocationMapActivity;
import com.example.ccj.b.a.OneMarkerLocation;
import com.example.ccj.b.c.dialog.LoadingDialog;
import com.example.ccj.u.ActivityUtils;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.ToastUtils;
import com.example.ccj.u.map.LocationListener;
import com.example.ccj.u.map.LocationMode;
import com.example.ccj.u.map.LocationUtil;
import com.example.ccj.u.message.UiMessageUtils;
import com.example.ccj.u.power.PowerPer;

import java.text.DecimalFormat;

import io.reactivex.ObservableEmitter;

/**
 * 定位
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/12/23 15:44
 */
public class FieldFor13Position extends BaseField {
    public FieldFor13Position(){
        super();
    }


    @Override
    public View getWritelView(Context context, WriteFormClass writeFormClass) {
        FormField formField = writeFormClass.formField;
        //提示文本
        if (formField.getHint() != null) writeFormClass.fieldFullContentView.setHint(formField.getHint());
        //输入文本颜色
        if (formField.getTextColor() != 0) writeFormClass.fieldFullContentView.setTextColor(formField.getTextColor());
        //是否只读
        if (formField.isReadOnly()) {
            writeFormClass.fieldFullContentView.setKeyListener(null);
            writeFormClass.fieldFullContentView.setFocusable(false);
        }

        //默认文本
        String textFieldZanCunData = getTextFieldZanCunData(context,"fieldId=?",String.valueOf(formField.getId()));
        if (textFieldZanCunData == null) textFieldZanCunData = formField.getDefaultContent();
        if (textFieldZanCunData != null) {
            writeFormClass.fieldFullContentView.setText(textFieldZanCunData);
            saveValue = textFieldZanCunData;
            writeFormClass.inputIsOK = true;
        }
        //文本改变事件
        writeFormClass.fieldFullContentView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                writeFormClass.inputIsOK = check(s,formField,writeFormClass.fieldFullWriteErrorView);
            }
        });

        writeFormClass.fieldFullJumpImgView.setVisibility(View.VISIBLE);
        writeFormClass.fieldFullJumpImgView.setImageResource(R.drawable.field_icon_chaolianjie);
        writeFormClass.fieldFullJumpImgView.setOnClickListener(getLocationOnClickListener(writeFormClass.fieldFullContentView));

        return writeFormClass.view;
    }

    @Override
    protected View getShowFullView(Context context, FullClass fullModel) {
        //字段名属性设置
        fullModel.setFieldProperty();
        //设置字段值
        fullModel.setContentProperty();

        String fieldValue = fullModel.getFieldValue();

        //具备修改权限时，点击右侧图标打开选择经纬度界面选择经纬度
        if (fullModel.isHaveModifyPower()) {
            fullModel.setJumpImageProperty(R.drawable.field_icon_chaolianjie, v -> {
                UiMessageUtils.getInstance().addListener(LocationMapActivity.RESULT_LOCATION_CODE, uiMessage -> {
                    Intent intent = (Intent) uiMessage.getObject();
                    if (intent != null) {
                        String stringExtra = intent.getStringExtra(LocationMapActivity.RESULT_SELECTED_LOCATION);
                        if (StringUtils.isNoEmpty(stringExtra)) {
                            fullModel.loadContent.setText(stringExtra);
                        }
                    }
                });
                //打开可获取定位的ACTIVITY
                Intent intent  = new Intent(v.getContext(), LocationMapActivity.class);
                intent.putExtra(LocationMapActivity.RESULT_SELECTED_LOCATION,fieldValue);
                ActivityUtils.getTopActivity().startActivityForResult(intent,LocationMapActivity.RESULT_LOCATION_CODE);
            });
        } else {
            //不具备修改权限时，点击右侧图标打开定位界面，显示当前定位
            fullModel.setJumpImageProperty(R.drawable.field_icon_chaolianjie, v -> {
                Intent intent  = new Intent(v.getContext(), OneMarkerLocation.class);
                String[] split = fieldValue.split(",");
                if (split.length == 2) {
                    //传入经纬度打开单点定位界面
                    intent.putExtra("jingdu",split[0]);
                    intent.putExtra("weidu",split[1]);
                    intent.putExtra("title",fullModel.formField.getName());
                    v.getContext().startActivity(intent);
                } else {
                    ToastUtils.showShort("经纬度格式不正确：" + fieldValue);
                }
            });
        }
        return fullModel.layout;
    }

    //获取经纬度点击事件
    private View.OnClickListener getLocationOnClickListener(TextView view){
        return v -> {
            //检查定位权限通过
            if (PowerPer.getLocationCoarsePower(v.getContext())) {
                LoadingDialog loadingDialog = new LoadingDialog(v.getContext());
                loadingDialog.showLoading();
                //获取经纬度
                try {
                    LocationUtil.getOneLocation(v.getContext(), new LocationListener() {
                        @Override
                        public void onResult(LocationMode locationMode) {
                            DecimalFormat f = new DecimalFormat("#.000000");
                            String j = f.format(locationMode.longitude);
                            String w = f.format(locationMode.latitude);
                            String lo = j + "," + w;
                            //将经纬度写入view
                            view.setText(lo);
                            saveValue = lo;
                            loadingDialog.dismiss();
                        }
                        @Override
                        public void onError(String s) {
                            loadingDialog.dismiss();
                            ToastUtils.showShort(s);
                        }
                    });
                } catch (Exception e) {
                    loadingDialog.dismiss();
                    ToastUtils.showShort(e.getMessage());
                }
            }
        };
    }



    /**
     * 字段设置界面
     * @param context 上下文
     * @param installClass 配置
     * @return 1
     */
    @Override
    protected View getFieldInstallView(Context context, InstallClass installClass) {
        ObservableEmitter<ViewGroup> emitter = installClass.emitter;

        emitter.onNext(installClass.getIsMastView());        //是否必填
        emitter.onNext(installClass.getIsMastInternetGetView()); //是否必须网络获取
        emitter.onNext(installClass.getIsPrintView());        //是否参与打印

        return installClass.v;
    }


}

